#include "../LeetCodeShell/common.h"

string convert(string s, int nRows)
{
	if(nRows<=1){
		return s;
	}
	
	char* buf = new char[s.size()+1];
	int p = 0;
	int half = nRows-1;
	int full = half*2;
	for(int i=0;i<s.size();i+=full){
			buf[p] = s[i];
			p++;
	}
	for(int r=1; r<half; r++){
		for(int b=0;b<s.size();b+=full){
			int left = b+r;
			int right = b+full-r;
			if(left<s.size()){
				buf[p] = s[left];
				p++;
			}
			if(right<s.size()){
				buf[p] = s[right];
				p++;
			}
		}
	}
	for(int i=half;i<s.size();i+=full){
		buf[p] = s[i];
		p++;
	}
	buf[p] = 0;

	string r(buf);
	delete [] buf;
	return r;
}
